package com.bookmanager.www.controller;

import cn.hutool.core.map.MapUtil;
import com.bookmanager.www.common.utils.Result;
import com.bookmanager.www.common.utils.ShiroUtils;
import com.bookmanager.www.common.vo.MenuListVO;
import com.bookmanager.www.entity.FlMenu;
import com.bookmanager.www.entity.end.EndMenu;
import com.bookmanager.www.mapper.FlMenuMapper;
import com.bookmanager.www.mapper.FlUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/11/20/0:06
 * @Description:
 */
@RestController
@RequestMapping("/fl-menu")
@Slf4j
@Api(tags = "菜单管理")
public class FlMenuController {
    @Resource
    FlUserMapper flUserMapper;
    @Resource
    FlMenuMapper flMenuMapper;


    @PostMapping("/allocateResources")
    @ApiOperation(value = "分配资源")
    @RequiresRoles(value = {"admin","superAdmin","超级管理员"},logical = Logical.OR)
    @Transactional(rollbackFor = Exception.class)
    Result allocateResources(@RequestParam("ids") String[] ids,@RequestParam("roleId") Integer roleId){
        //不能给超级管理员分配菜单

        //首先删除原有的资源 然后新增资源

        int delete = flMenuMapper.deleteResourcesByRoleId(roleId);
        log.warn("删除资源数量：{}",delete);

        for(String id : ids){
            int allocate = flMenuMapper.allocateResources(id,roleId);
            log.warn("分配资源数量：{}",allocate);
            if(allocate == 0){
                throw new RuntimeException("分配资源失败"); //回滚
//                return Result.fail("分配资源失败","");
            }
        }
        return Result.succ("分配资源成功","");



    }

    @GetMapping("/getMenuList")
    @ApiOperation(value = "获取菜单列表")
    @RequiresRoles(value = {"admin","superAdmin","超级管理员","商品管理员","订单管理员"},logical = Logical.OR)
    Result getMenuList()
    {
        String userId = ShiroUtils.getUserId();
        log.warn("获取菜单列表，userId={}",userId);

        //获取用户角色
        Integer roleId = flUserMapper.getRoleIdByUserId(userId);
        log.warn(roleId.toString());
        List<FlMenu> menuList = flMenuMapper.getMenuListByRoleId(roleId);
        log.warn(menuList.toString());
        List<MenuListVO> menuListVOList =new LinkedList<>();
        for (FlMenu flMenu : menuList) {
            List<FlMenu> menuList2 = flMenuMapper.getMenuListByFatherId(flMenu.getMenuId());
//            MenuListVO menuListVO = new MenuListVO();
//            menuListVO.setFlMenu(flMenu);
//            LinkedHashMap<String, List<FlMenu>> children =new LinkedHashMap<>();
//            children.put("children",menuList2);
//            menuListVO.setMenuMap(children);
//            menuListVOList.add(menuListVO);
            flMenu.setChildren(menuList2);
        }

        return Result.succ("获取菜单列表成功",menuList);
    }

    @GetMapping("/getMenuList2")
    @ApiOperation(value = "获取菜单列表new")
    @RequiresRoles(value = {"admin","superAdmin","超级管理员","商品管理员","订单管理员"},logical = Logical.OR)
    Result getMenuList2()
    {
        String userId = ShiroUtils.getUserId();

        Integer roleId = flUserMapper.selectRoleIdByUserId(userId);

        List<EndMenu> endMenuList = flMenuMapper.selectEndMenuListByRoleId(roleId);

        for (EndMenu endMenu : endMenuList) {
            List<EndMenu> menuList2 = flMenuMapper.selectMenuListByFatherId(endMenu.getId());
            endMenu.setChildren(menuList2);
        }

        return Result.succ("获取菜单列表成功",endMenuList);

    }


    @PutMapping("/editMenuItem")
    @ApiOperation(value = "编辑菜单项")
    @RequiresRoles(value = {"admin","superAdmin","超级管理员"},logical = Logical.OR)
    Result editMenuItem(@RequestBody EndMenu endMenu){

        if(endMenu.getId()==null){
            return Result.fail("编辑菜单项失败，id不能为空","");
        }
        if(endMenu.getParentId()==null){
            return Result.fail("编辑菜单项失败，parentId不能为空","");
        }
        if (endMenu.getTitle()==null){
            return Result.fail("编辑菜单项失败，title不能为空","");
        }
        if(endMenu.getHidden()==null){
            return Result.fail("编辑菜单项失败，hidden不能为空","");
        }
        if(endMenu.getSort()==null){
            return Result.fail("编辑菜单项失败，sort不能为空","");
        }
        if(endMenu.getName()==null)
        {
            return Result.fail("编辑菜单项失败，路径不能为空","");
        }

       int update =  flMenuMapper.updateMenuItem(endMenu);

       if(update>0){
        return Result.succ("编辑菜单项成功","");
                }

      return Result.fail("编辑菜单项失败","");
    }

    @DeleteMapping("/deleteMenuItem")
    @ApiOperation(value = "删除菜单项")
    @RequiresRoles(value = {"admin","superAdmin","超级管理员"},logical = Logical.OR)
    Result deleteMenuItem(@RequestParam("id") Integer id){

        int parentId = flMenuMapper.selectParentIdByMenuId(id);

        if(parentId==0){
            return Result.fail("删除失败，该菜单项为父菜单项","");
        }

        int delete = flMenuMapper.deleteMenuItem(id);

        if(delete>0){
            return Result.succ("删除菜单项成功","");
        }

        return Result.fail("删除菜单项失败","");
    }

    @PutMapping("/addMenuItem")
    @ApiOperation(value = "添加菜单项")
    @RequiresRoles(value = {"admin","superAdmin","超级管理员"},logical = Logical.OR)
    Result addMenuItem(@RequestBody EndMenu endMenu){

//        if(endMenu.getParentId()==0){
//            return Result.fail("添加失败，该菜单项为父菜单项","");
//        }

        int insert = flMenuMapper.insertMenuItem(endMenu);

        if(insert>0){
            return Result.succ("添加菜单项成功","");
        }

        return Result.fail("添加菜单项失败","");
    }


}
